package com.zhz.mall.admin.modules.pms.controller;


import com.zhz.mall.admin.modules.pms.model.PmsProductAttribute;
import com.zhz.mall.admin.param.ProductAttributeDeleteByIdsParam;
import com.zhz.mall.common.api.CommonPage;
import com.zhz.mall.common.api.CommonResult;
import com.zhz.mall.admin.dto.RelationAttrInfoDTO;
import com.zhz.mall.admin.modules.pms.service.PmsProductAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商品属性参数表 前端控制器
 * </p>
 *
 * @author zhouhengzhe
 * @since 2023-01-09
 */
@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {

    @Autowired
    private PmsProductAttributeService productAttributeService;

    /**
     * 商品分类—商品属性数据列表
     * url:'/productAttribute/list/'+cid,
     * method:'get',
     * params:params
     */
    @GetMapping("/list/{cid}")
    public CommonResult<CommonPage<PmsProductAttribute>> getList(@PathVariable Long cid,
                                                                 @RequestParam(value = "type") Integer type,
                                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                 @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        return CommonResult.success(CommonPage.restPage(productAttributeService.list(cid, type, pageNum, pageSize)));
    }

    /**
     * 属性添加
     * return request({
     * url:'/productAttribute/create',
     * method:'post',
     * data:data
     * })
     */
    @PostMapping("/create")
    public CommonResult<Boolean> create(@RequestBody PmsProductAttribute productAttribute) {
        return productAttributeService.create(productAttribute) ? CommonResult.success(true) : CommonResult.failed();
    }

    /**
     * 属性修改
     * url:'/productAttribute/update/'+id,
     * method:'post',
     * data:data
     */
    @PostMapping("/update/{id}")
    public CommonResult<Boolean> update(@RequestBody PmsProductAttribute productAttribute) {
        return productAttributeService.updateById(productAttribute) ? CommonResult.success(true) : CommonResult.failed();

    }

    /**
     * 根据id获取商品属性
     * url:'/productAttribute/'+id,
     * method:'get'
     */
    @GetMapping( "/{id}")
    public CommonResult<PmsProductAttribute> getById(@PathVariable Long id) {
        return CommonResult.success(productAttributeService.getById(id));
    }

    /**
     * 删除商品属性
     * url:'/productAttribute/delete',
     * method:'post',
     * data:data
     */
    @PostMapping("/delete")
    public CommonResult<Boolean> delete(@Valid @RequestBody ProductAttributeDeleteByIdsParam param) {
        return productAttributeService.delete(param.getIds()) ? CommonResult.success(true) : CommonResult.failed();
    }

    /**
     * 根据商品分类id获取关联的筛选属性
     * url:'/productAttribute/attrInfo/'+productCategoryId,
     * method:'get'
     */
    @GetMapping("/attrInfo/{cId}")
    public CommonResult<List<RelationAttrInfoDTO>> getRelationAttrInfoByCid(@PathVariable Long cId) {
        return CommonResult.success(productAttributeService.getRelationAttrInfoByCid(cId));
    }
}

